package com.diandian.backend.douyin.life.exchange.controller;

import com.diandian.backend.douyin.life.exchange.req.MemberMobileChangeReq;
import com.diandian.backend.douyin.life.exchange.req.MemberPointsChangeReq;
import com.diandian.backend.douyin.life.exchange.req.MemberUnbindChangeReq;
import com.diandian.backend.douyin.life.exchange.service.DouyinLifeTokenBusinessService;
import com.diandian.backend.douyin.life.exchange.service.MemberInfoChangeService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 抖音本地生活会员数据交互API
 */
@RestController
@RequestMapping("/member")
public class DouyinLifeMemberController {

    @Autowired
    private MemberInfoChangeService memberInfoChangeService;

    @Autowired
    private DouyinLifeTokenBusinessService tokenBusinessService;

    /**
     * 积分等级更新
     * @return
     */
    @PostMapping("/points/change")
    public BaseResult pointsChange(@RequestHeader(value = "clientKey") String clientKey,
                                   @RequestHeader(value = "clientSecret") String clientSecret,
                                   @RequestHeader(value = "account") String account,
                                   @RequestBody MemberPointsChangeReq req) {
        String token = tokenBusinessService.getToken(clientKey, clientSecret);
        return ResultUtil.makeResult(memberInfoChangeService.pointsChange(token, account, req));
    }

    /**
     * 手机号更新
     * {
     *     "appKey":"lingfang.test.appKey",
     *     "appSecret":"lingfang.test.appSecret",
     *     "time":"2025-06-23 14:44:00",
     *     "paramJson":"{\"openId\":\"_000x9QiZFSRZkO5Ouf9SiPqwOsHU85udy-l\",\"newMobile\":\"15069028961\",\"updateTime\":null}",
     *     "sign":"792f882c72e85cb04078d84914391635"
     * }
     * @param req
     * @return
     */
    @PostMapping("/mobile/change")
    public BaseResult mobileChange(@RequestHeader(value = "clientKey") String clientKey,
                               @RequestHeader(value = "clientSecret") String clientSecret,
                               @RequestHeader(value = "account") String account,
                               @RequestBody MemberMobileChangeReq req) {
        // 获取token
        String token = tokenBusinessService.getToken(clientKey, clientSecret);
        return ResultUtil.makeResult(memberInfoChangeService.mobileChange(token, account, req));
    }

    /**
     * 用户解绑
     * {
     *     "appKey":"lingfang.test.appKey",
     *     "appSecret":"lingfang.test.appSecret",
     *     "time":"2025-06-23 14:44:00",
     *     "paramJson":"{\"openId\":\"_000x9QiZFSRZkO5Ouf9SiPqwOsHU85udy-l\",\"updateTime\":null}",
     *     "sign":"792f882c72e85cb04078d84914391635"
     * }
     * @param req
     * @return
     */
    @PostMapping("/unbind")
    public BaseResult unbind(@RequestHeader(value = "clientKey") String clientKey,
                         @RequestHeader(value = "clientSecret") String clientSecret,
                         @RequestHeader(value = "account") String account,
                         @RequestBody MemberUnbindChangeReq req) {
        // 获取token
        String token = tokenBusinessService.getToken(clientKey, clientSecret);
        return ResultUtil.makeResult(memberInfoChangeService.unbindChange(token, account, req));
    }
}
